<?php
/**************************************************************************
 * Naanal PHP Framework, Simple, Efficient and Developer Friendly
 * Copyright (C) <2010>  <Tamil Amuthan. R>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ************************************************************************/

//trace($form->get_adjustmentgrid_lookup());
$formvar_settings=array
(
	"radius"=>$form->get_data("radius"),
	"unit"=>array
	(
		"option"=>$form->get_lookup("Unit of Distance"),
		"value"=>$form->get_data("Unit of Distance")
	),
	"unit_of_currency"=>$form->get_data("Unit of Currency"),
	"distance_sorting"=>array
	(
		"option"=>$form->get_lookup("Distance Sorting"),
		"value"=>$form->get_data("Distance Sorting")
	),
	"sale_date_order"=>array
	(
		"option"=>$form->get_lookup("Sale Date Order"),
		"value"=>$form->get_data("Sale Date Order")
	),
	"adjusted_value_order"=>array
	(
		"option"=>$form->get_lookup("Adjusted Value Order"),
		"value"=>$form->get_data("Adjusted Value Order")
	),
	"default_adjustment_grid"=>array
	(
		"option"=>$form->get_adjustmentgrid_lookup(),
		"value"=>$form->get_data("Adjustment Grid")
	),
	"zoom_level"=>$form->get_data("Zoom Level"),
	"scale_level"=>$form->get_data("Scale Level"),
	/*"comps_search_criteria"=>array
	(
		"option"=>$form->get_lookup("Comps Search Criteria"),
		"value"=>$form->get_data("Comps Search Criteria")
	),*/
	"unit_of_measure"=>array
	(
		"option"=>$form->get_lookup("Unit of Measure"),
		"value"=>$form->get_data("Unit of Measure")
	),
	"hidden"=>array(
		"page"=>"settings",
		"issubmit"=>getSessionRandomNumber("create"),
		"action"=>"insert"
	)
);
$arrCompsField=array();
$objAdjustmentAttribute=new ClsWAdjustmentattribute();
$arrColumn=$objAdjustmentAttribute->getList(array("caption","islistable"));
$arrMultiColumn=getMultiColumn($arrColumn,4,"fldcheck");
if($arrMultiColumn)
foreach($arrMultiColumn as $ind=>$arrRow)
{
	foreach($arrRow as $var=>$arrCol)
	{
		$repeat_fldcomp[$ind][$var]["attr"]["name"]="field[".$arrCol["id"]."]";
		$repeat_fldcomp[$ind][$var]["attr"]["value"]=$arrCol["id"];
		$suf=substr($var, 8);
		$repeat_fldcomp[$ind]["fldcomp".$suf]["text"]=$arrCol["caption"];
		if($arrCol["islistable"]>0)
			$repeat_fldcomp[$ind][$var]["attr"]["checked"]="checked";
	}
}

$objAA=new ClsAdjustmentAttribute();
$fldNeighborhood=$objAA->getNeighborhoodField();
$sql="select distinct `{$fldNeighborhood}` from comps where `{$fldNeighborhood}`!=''";
$pdo=ClsNaanalPDO::getNamedInstance(getDynamicDatabase());
$pdo->setQuery($sql);
$arr=$pdo->getAllAssoc();

foreach($arr as $ind=>$r)
{
	$sql="select neighborhood.value,neighborhood.score from neighborhood where neighborhood.value='{$r[$fldNeighborhood]}'";
	$pdo->setQuery($sql);
	$arrN=$pdo->getAllAssoc();
	$score=isset($arrN[0]["score"])?$arrN[0]["score"]:"";
	$value=isset($arrN[0]["value"])?$arrN[0]["value"]:"";
	$repeat_listneighborhood[$ind]["neighborhood"]["text"]=$r[$fldNeighborhood];
	$repeat_listneighborhood[$ind]["score"]["attr"]["name"]="score[".$r[$fldNeighborhood]."]";
	$repeat_listneighborhood[$ind]["score"]["attr"]["value"]=$score;
}

$scriptvar_initjs='';

//$objWAA=new ClsWAdjustmentattribute();
$sql="select * from adjustmentattribute";
$pdo->setQuery($sql);
$arrA=$pdo->getAllAssoc();
//$arrObj=$objWAA->getList();trace($arrObj);

$jsonCriteria=$form->get_data("Comps Search Criteria");
$arrCriteria=json_decode($jsonCriteria,true);

if($arrA)
foreach($arrA as $ind=>$row)
{
	$caption=$row["caption"];
	if($row["type"]=="neighborhood" || $row["type"]=="qualitative")
	{
		$repeat_search_criteria[$ind]["compfield"]["text"]=$caption;
		$repeat_search_criteria[$ind]["condition"]["text"]='<select rel="'.$row["type"].'" id="criteria_'.$row["id"].'" name="criteria['.$row["id"].']">
		<option value="">All</option>
		<option value="same_as_subject" '.(isset($arrCriteria["criteria"][$row["id"]]) && $arrCriteria["criteria"][$row["id"]]=="same_as_subject"?"selected":"").'>Same as Subject</option>
		</select>
		';
	}
	else if($row["type"]=="quantitative")
	{
		$criteriavalue=isset($arrCriteria["criteriavalue"][$row["id"]])?$arrCriteria["criteriavalue"][$row["id"]]:"";
		$scriptvar_initjs=$scriptvar_initjs.'
		var obj=document.getElementById("criteria_'.$row["id"].'");
		loadInput(obj,"quantitative",'.$row["id"].',"'.$criteriavalue.'");
		';
		$repeat_search_criteria[$ind]["compfield"]["text"]=$caption;
		$repeat_search_criteria[$ind]["condition"]["text"]='<select  rel="'.$row["type"].'" onchange="loadInput(this,\'quantitative\','.$row["id"].')" id="criteria_'.$row["id"].'" name="criteria['.$row["id"].']">
		<option value="">All</option>
		<option value="plusorminus" '.(isset($arrCriteria["criteria"][$row["id"]]) && $arrCriteria["criteria"][$row["id"]]=="plusorminus"?"selected":"").'>+/- of Subject</option>
		<option value="plusorminuspercent" '.(isset($arrCriteria["criteria"][$row["id"]]) && $arrCriteria["criteria"][$row["id"]]=="plusorminuspercent"?"selected":"").'>% +/- of Subject</option>
		</select>
		';
	} 
	else if($row["type"]=="sale date")
	{
		$criteriavalue=isset($arrCriteria["criteriavalue"][$row["id"]]["from"])?$arrCriteria["criteriavalue"][$row["id"]]["from"]:"";
		$criteriavalueto=isset($arrCriteria["criteriavalue"][$row["id"]]["to"])?$arrCriteria["criteriavalue"][$row["id"]]["to"]:"";
		$scriptvar_initjs=$scriptvar_initjs.'
		var obj=document.getElementById("criteria_'.$row["id"].'");
		loadInput(obj,"sale date",'.$row["id"].',"'.$criteriavalue.'","'.$criteriavalueto.'");
		';
		$repeat_search_criteria[$ind]["compfield"]["text"]=$caption;
		$repeat_search_criteria[$ind]["condition"]["text"]='<select id="criteria_'.$row["id"].'" rel="'.$row["type"].'" onchange="loadInput(this,\'sale date\','.$row["id"].')" name="criteria['.$row["id"].']">
		<option value="">All</option>
		<option value="less" '.(isset($arrCriteria["criteria"][$row["id"]]) && $arrCriteria["criteria"][$row["id"]]=="less"?"selected":"").'>Lesser Than</option>
		<option value="great" '.(isset($arrCriteria["criteria"][$row["id"]]) && $arrCriteria["criteria"][$row["id"]]=="great"?"selected":"").'>Greater Than</option>
		<option value="between" '.(isset($arrCriteria["criteria"][$row["id"]]) && $arrCriteria["criteria"][$row["id"]]=="between"?"selected":"").'>Between</option>
		</select>
		';
	}
}

$date='<select name="month">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>';

$month='<select name="month">
<option value="jan">Jan</option>
<option value="feb">Feb</option>
<option value="mar">Mar</option>
<option value="apr">Apr</option>
<option value="may">May</option>
<option value="jun">Jun</option>
<option value="jul">Jul</option>
<option value="aug">Aug</option>
<option value="sep">Sep</option>
<option value="oct">Oct</option>
<option value="nov">Nov</option>
<option value="dec">Dec</option>
</select>';

$repeat_calendar_control[0]["calendar"]["text"]="Assesment Year Start Date";
$repeat_calendar_control[0]["date"]["text"]=$date;
$repeat_calendar_control[0]["month"]["text"]=$month;

$repeat_calendar_control[1]["calendar"]["text"]="Assesment Year End Date";
$repeat_calendar_control[1]["date"]["text"]=$date;
$repeat_calendar_control[1]["month"]["text"]=$month;

$repeat_calendar_control[2]["calendar"]["text"]="Current Assesment Date";
$repeat_calendar_control[2]["date"]["text"]=$date;
$repeat_calendar_control[2]["month"]["text"]=$month;

$repeat_calendar_control[3]["calendar"]["text"]="Cutoff date for current year";
$repeat_calendar_control[3]["date"]["text"]=$date;
$repeat_calendar_control[3]["month"]["text"]=$month;	
?>